home *** CD-ROM | disk | FTP | other *** search
/ OneVision 3.03 / OneVision3.03.iso / Apps / Convert PICT.app / CommentedPSCode / RoundRectangles < prev    next >
Encoding:
Text File  |  1993-11-08  |  6.4 KB  |  286 lines

  1. %BEGIN ReoundRectangles
  2.  
  3. % Copyright (C) 1993 David John Burrowes
  4. % Distributed under terms of GNU General Public License.
  5. % See COPYING.text in Convert PICT's CommentedPSCode for a copy
  6.  
  7. %%%%%%%%%%%%%
  8. %    Define the default values to be used for the width and height of the
  9. %    cuves on the corners of the round rectangles.
  10. %%%%%%%%%%%%%
  11. /ovWidth 0 def
  12. /ovHeight 0 def
  13.  
  14. %%%%%%%%%%%%%
  15. %    Name:    ovSize            [000B]
  16. %    Syntax:    width height ovSize -
  17. %    About:    Sets the width and height of curves on round-rectangle corners
  18. %%%%%%%%%%%%%
  19. /ovSize
  20. {
  21.     /ovHeight exch def
  22.     /ovWidth exch def
  23. }
  24. def
  25.  
  26. %%%%%%%%%%%%%
  27. %    Name:    buildRRpath
  28. %    Syntax:    t l b r buildRRpath -
  29. %    About:    Given a rectangle, build a round-rect path in that rect
  30. %            using the values of ovWidth and ovHeight.  This necessarily
  31. %            distorts userspace to get the ovaled corners.
  32. %            Note: This assumes there is a current path.  It does not
  33. %            create a new one because it is called 2ce by frameRRect
  34. %            Remove trapping for big oval sizes for pretty results
  35. %%%%%%%%%%%%%
  36. /buildRRpath
  37. {
  38.     /rightCoord exch def
  39.     /bottomCoord exch def
  40.     /leftCoord exch def
  41.     /topCoord exch def
  42.  
  43.     /startmatrix  matrix currentmatrix def
  44.     %
  45.     %    Compute half ovheight and half ovwith, trapping for (a) case when
  46.     %    width or height larger than corresponding rect dimension, and
  47.     %    (b) cases when ovwidth or ovheight is 0. In case of (b), we make
  48.     %    the half value non-zero, but very small.  We divide by this value
  49.     %    later, so a 0 value would be bad.  
  50.     %
  51.     ovHeight 0 le
  52.     { /halfheight .0001 def }
  53.     {
  54.         ovHeight bottomCoord topCoord sub gt
  55.             {/halfheight bottomCoord topCoord sub 2 div def}
  56.             {/halfheight ovHeight 2 div def}
  57.         ifelse
  58.     }
  59.     ifelse
  60.  
  61.     ovWidth 0 le
  62.     { /halfwidth .0001 def }
  63.     {
  64.         ovWidth rightCoord leftCoord sub gt
  65.             {/halfwidth rightCoord leftCoord sub 2 div def}
  66.             {/halfwidth ovWidth 2 div def}
  67.         ifelse
  68.     }
  69.     ifelse
  70.     %
  71.     %    Distort space properly for ovals, and compute where the
  72.     %    rectangle edges should be in this distorted space (so they
  73.     %    lie on the same screen locations despite distortion)
  74.     %
  75.     halfwidth halfheight gt
  76.     {
  77.         /unscale halfwidth halfheight div def
  78.         1 halfheight halfwidth div  scale
  79.         /radius halfwidth def
  80.         /newleft leftCoord def
  81.         /newright rightCoord def
  82.         /newtop topCoord unscale mul def
  83.         /newbottom bottomCoord unscale mul def
  84.     }
  85.     {
  86.         /unscale halfheight halfwidth div def
  87.         halfwidth halfheight div 1 scale
  88.         /radius halfheight def
  89.         /newleft leftCoord unscale mul  def
  90.         /newright rightCoord unscale mul def
  91.         /newtop topCoord def
  92.         /newbottom bottomCoord def
  93.     }
  94.     ifelse
  95.     %
  96.     %    Finally, draw the round rect using arct which draws both an
  97.     %    arced corner, as well as the edge to it.
  98.     %
  99.     newleft halfwidth add newtop moveto
  100.     newright newtop newright newbottom radius arct
  101.     newright newbottom newleft newbottom radius arct
  102.     newleft newbottom newleft newtop radius arct
  103.     newleft newtop newright newtop radius arct
  104.     closepath
  105.     startmatrix setmatrix
  106. }
  107. def
  108.  
  109.  
  110. %%%%%%%%%%%%%
  111. %    Name:    roundrectpath
  112. %    Syntax:    t l b r roundrectpath -
  113. %    About:    Provides a wrapper around buildRRpath so we can declare
  114. %            a newpath first.  Note that we define last* values here for
  115. %            all the routines below that call this.
  116. %%%%%%%%%%%%%
  117. /roundrectpath
  118. {
  119.     /lastright exch  def
  120.     /lastbottom exch  def
  121.     /lastleft exch def
  122.     /lasttop exch def
  123.  
  124.     newpath
  125.     lasttop lastleft lastbottom lastright buildRRpath
  126. }
  127. def
  128.  
  129. %%%%%%%%%%%%%
  130. %    Name:    frameRRect                [0040]
  131. %    Syntax:    t l b r frameRRect -
  132. %    About:    Frames the specified round rect. If the pen sizes are 1,
  133. %            stroke the path. Otherwise, build an inner path, and
  134. %            fill between them.
  135. %%%%%%%%%%%%%
  136. /frameRRect
  137. {
  138.     /lastright exch  def
  139.     /lastbottom exch  def
  140.     /lastleft exch def
  141.     /lasttop exch def
  142.     %
  143.     %    Do something if pen sizes > 0
  144.     %
  145.     penWidth 0 gt
  146.     penHeight 0 gt
  147.     and
  148.     {
  149.         gsave
  150.             penPattern usePattern
  151.             newpath
  152.             penWidth 1 eq
  153.             penHeight 1 eq
  154.             and
  155.             {
  156.                 lasttop lastleft lastbottom 1 sub  lastright 1 sub  buildRRpath
  157.                 stroke
  158.             }
  159.             {
  160.                 lasttop lastleft lastbottom lastright  buildRRpath
  161.                 %
  162.                 %    Compute parameters for inner edge of rrect frame.
  163.                 %    Note: we temporarily change ovWidth & ovHeight to
  164.                 %    produce aesthetically pleasing inner curves.
  165.                 %
  166.                 save
  167.                     /ovHeight ovHeight penHeight 2 mul sub def
  168.                     /ovWidth ovWidth penWidth 2 mul sub def
  169.                     lasttop penHeight add
  170.                     lastleft penWidth add
  171.                     lastbottom penHeight sub 
  172.                     lastright penWidth sub
  173.                     buildRRpath
  174.                     eofill
  175.                 restore
  176.             }
  177.             ifelse
  178.         grestore
  179.     }
  180.     if
  181. } def
  182.  
  183. %%%%%%%%%%%%%
  184. %    Name:    paintRRect            [0041]
  185. %    Syntax:    t l b r  paintRRect -
  186. %    About:    Fills the path of a round rectangle with the pen pattern
  187. %%%%%%%%%%%%%
  188. /paintRRect
  189. {
  190.     gsave
  191.         penPattern usePattern
  192.         roundrectpath
  193.         fill
  194.     grestore
  195. }
  196. def
  197.  
  198. %%%%%%%%%%%%%
  199. %    Name:    eraseRRect            [0042]
  200. %    Syntax:    t l b r  eraseRRect -
  201. %    About:    Fills the path of a round rectangle with the background pattern
  202. %%%%%%%%%%%%%
  203. /eraseRRect
  204. {
  205.     gsave
  206.         backPattern usePattern
  207.         roundrectpath
  208.         fill
  209.     grestore
  210. }
  211. def
  212.  
  213. %%%%%%%%%%%%%
  214. %    Name:    invertRRect            [0043]
  215. %    Syntax:    t l b r  invertRRect -
  216. %    About:    Calls roundrectpath to properly consume parameters.
  217. %            We don't know how to invert it, though, so don't.
  218. %%%%%%%%%%%%%
  219. /invertRRect
  220. {
  221.     gsave
  222.         roundrectpath
  223.     grestore
  224. } def
  225.  
  226. %%%%%%%%%%%%%
  227. %    Name:    fillRRect            [0044]
  228. %    Syntax:    t l b r  fillRRect -
  229. %    About:    Fills the path of a round rectangle with the fill pattern
  230. %%%%%%%%%%%%%
  231. /fillRRect
  232. {
  233.     gsave
  234.         fillPattern usePattern
  235.         roundrectpath
  236.         fill
  237.     grestore
  238. } def
  239.  
  240. %%%%%%%%%%%%%
  241. %    Name:    frameSameRRect            [0048]
  242. %    Syntax:    - frameSameRRect -
  243. %    About:    Frames the last roundrectangle used
  244. %%%%%%%%%%%%%
  245. /frameSameRRect
  246.     { lasttop lastleft lastbottom lastright frameRRect }
  247. def
  248.  
  249. %%%%%%%%%%%%%
  250. %    Name:    paintSameRRect            [0049]
  251. %    Syntax:    - paintSameRRect -
  252. %    About:    Paints the last roundrectangle used
  253. %%%%%%%%%%%%%
  254. /paintSameRRect
  255.     { lasttop lastleft lastbottom lastright paintRRect }
  256. def
  257.  
  258. %%%%%%%%%%%%%
  259. %    Name:    eraseSameRRect            [004A]
  260. %    Syntax:    - eraseSameRRect -
  261. %    About:    Erases the last roundrectangle used
  262. %%%%%%%%%%%%%
  263. /eraseSameRRect
  264.     { lasttop lastleft lastbottom lastright eraseRRect }
  265. def
  266.  
  267. %%%%%%%%%%%%%
  268. %    Name:    invertSameRRect            [004B]
  269. %    Syntax:    - invertSameRRect -
  270. %    About:    Inverts the last roundrectangle used
  271. %%%%%%%%%%%%%
  272. /invertSameRRect
  273.     { lasttop lastleft lastbottom lastright invertRRect }
  274. def
  275.  
  276. %%%%%%%%%%%%%
  277. %    Name:    fillSameRRect            [004C]
  278. %    Syntax:    - fillSameRRect -
  279. %    About:    Fills the last roundrectangle used
  280. %%%%%%%%%%%%%
  281. /fillSameRRect
  282.     { lasttop lastleft lastbottom lastright fillRRect }
  283. def
  284.  
  285. %END RoundRectangles
  286.